package com.apesource.mallproject.dao;

import com.apesource.mallproject.pojo.Brand;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

@Repository
public interface IBrandDao {
    //查询所有品牌
    @Select("select * from brand order by id desc")
    public List<Brand> queryAllBrand();

    //新增品牌
    @Insert("insert into brand values(null,#{name},#{jPing})")
    public void insertBrand(@Param("name") String name, @Param("jPing") String jPing);

    //修改所有品牌
    @Update("update brand set name=#{name},jping=#{jPing} where id = #{id}")
    void updateBrand(@Param("name") String name, @Param("jPing") String jPing, @Param("id") Integer id);

    //删除一个
    @Delete("delete from brand where id = #{id}")
    void delBrand(@Param("id") Integer id);

    //批量删除
    @DeleteProvider(type = brandProvider.class, method = "batchDelete")
    int batchDelete(String[] ids);

    class brandProvider {
        public String batchDelete(Map map) {
            String[] ids = (String[]) map.get("array");
            StringBuilder sb = new StringBuilder();
            sb.append("DELETE FROM brand WHERE id IN (");
            for (int i = 0; i < ids.length; i++) {
                sb.append("'").append(Integer.parseInt(ids[i])).append("'");
                if (i < ids.length - 1)
                    sb.append(",");
            }
            sb.append(")");
            return sb.toString();
        }

    }


}
